# ... existing code ...

def debate_generator(topic, history: list, state: dict):
    topic = topic.strip()
    if topic == "":
        return [], state
    controller = DebateController(topic, debate_mode="human_vs_ai" if state.get('human_mode') else "ai_vs_ai")
    state["debate_state"] = controller.get_debates_info()
    current_role = "assistant"

    try:
        for speaker, message in controller.run_debate():
            if state['running'] is False:
                break

            # 新增人类输入判断
            if speaker == "human_turn":
                state['waiting_input'] = True  # 设置等待输入标志
                yield history, state, None, state['debate_state'][0], state['debate_state'][1]
                return  # 暂停生成器，等待用户输入后重新启动

            # 原有音频处理逻辑...

    except Exception as e:
        yield history, state, None, None, None


# 在辩论流程运行逻辑中
debate_process = controller.run_debate()
while True:
    speaker, content = next(debate_process)
    if speaker == "human_turn":
        # 显示人类输入界面
        show_human_interface()
    else:
        # 正常处理AI发言
        update_chatbot(speaker, content)

# 新增人类输入处理函数
def handle_human_input(human_text, history, state):
    if state.get('waiting_input'):
        # 将人类输入加入历史
        history.append({"role": "user", "content": human_text})
        state['waiting_input'] = False
        # 重新启动生成器
        return debate_generator(state['topic'], history, state)
    return history, state, None, None, None

# 修改事件绑定链
start_btn.click(
    start_debate,
    inputs=[debate_state, is_play],
    outputs=[...],
    queue=False
).then(
    lambda topic: gr.update(visible=True) if topic else gr.update(),
    inputs=debate_topic,
    outputs=human_interface
).then(
    debate_generator,
    inputs=[debate_topic, debate_screen, debate_state],
    outputs=[debate_screen, debate_state, soundfile, pro_speakers, con_speakers]
)

# 新增发送按钮事件绑定
send_btn.click(
    handle_human_input,
    inputs=[human_input, debate_screen, debate_state],
    outputs=[debate_screen, debate_state, soundfile, pro_speakers, con_speakers]
)
